package com.supereye.platform.web.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.supereye.platform.common.PageData;
import com.supereye.platform.util.EncodeUtil;
import com.supereye.platform.util.JacksonUtil;
import com.supereye.platform.web.sys.dao.SysUserDao;
import com.supereye.platform.web.sys.model.SysUser;
import com.supereye.platform.web.sys.model.SysUserAuth;

@Service
public class SysUserService {
	@Resource
	private SysUserDao sysUserDao;

	public void addUser(SysUser user) throws Exception {
		user.setPassword(EncodeUtil.toMd5(user.getPassword()));
		sysUserDao.addUser(user);
	}

	public void updateUser(SysUser user) throws Exception {
		sysUserDao.updateUser(user);
	}

	public String getUserList(SysUser user) throws Exception {
		List<SysUser> list = sysUserDao.getUserList(user);
		PageData<SysUser> pageData = new PageData<SysUser>();
		//pageData.setTotal(28);
		pageData.setRows(list);
		return JacksonUtil.toJSon(pageData);
	}

	public void deleteUsers(String[] userIds) throws Exception {
		sysUserDao.deleteUsers(userIds);
	}

	public String getUserAuthList(int orgId) throws Exception {
		List<SysUser> list = sysUserDao.getUserAuthList(orgId);
		PageData<SysUser> pageData = new PageData<SysUser>();
		//pageData.setTotal(28);
		pageData.setRows(list);
		return JacksonUtil.toJSon(pageData);
	}

	public void addUserAuth(Integer orgId, String userIds) throws Exception {
		SysUserAuth auth = null;
		String[] ids = userIds.split(",");
		for(String id:ids){
			auth = new SysUserAuth();
			auth.setOrgId(orgId);
			auth.setUserId(Integer.parseInt(id));
			sysUserDao.addUserAuth(auth);
		}
	}

	public void deleteUserAuth(Integer orgId,String userIds) throws Exception {
		SysUserAuth auth = null;
		String[] ids = userIds.split(",");
		for(String id:ids){
			auth = new SysUserAuth();
			auth.setOrgId(orgId);
			auth.setUserId(Integer.parseInt(id));
			sysUserDao.deleteUserAuth(auth);
		}
	}

	public String getUserListNotAuth() throws Exception {
		List<SysUser> list = sysUserDao.getUserListNotAuth();
		PageData<SysUser> pageData = new PageData<SysUser>();
		pageData.setRows(list);
		return JacksonUtil.toJSon(pageData);
	}
	
	public void updatePassword(int id,String newpassword) throws Exception {
		sysUserDao.updatePassword(id,newpassword);
	}

}
